﻿using System;
using System.Collections.Generic;
using System.Linq;  
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//-----------
using System.Web.Security;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
using System.Data.OleDb;

public partial class login : System.Web.UI.Page
{     

    private bool loginCheck()
    {
       
       
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
        SqlConnection conn = new SqlConnection(constr);

        //string sel = "";
        int logintype=-1;
       
        string userid=null;
        string hotelid = null; 
         conn.Open();
        if(DropDownList_logintype.SelectedValue.ToString()=="agent")
        {
            //sel = "SELECT Agent.agentID FROM Agent WHERE Agent.agentID="
            //+ SqlFormat.ap(TextBox_username.Text)
            //+ "or Agent.IDcard="
            //+ SqlFormat.ap(TextBox_username.Text)
            //+ " and Agent.acpassword=" 
            //+ SqlFormat.ap(TextBox_password.Text) + ";";
            object[] data=Agent.getDataByIDAndPsw(conn,TextBox_username.Text,TextBox_password.Text);

            if(data!=null&&data.Length>0){
                
                userid=data[0].ToString();
            }
            
            logintype = 1;
        }
        else if (DropDownList_logintype.SelectedValue.ToString() == "hotel")
        {
            //sel = "SELECT Hotel.hotelID FROM Hotel WHERE Hotel.hotelID="
            //+ SqlFormat.ap(TextBox_username.Text)
            //+ "or Hotel.telephonenumber="
            //+ SqlFormat.ap(TextBox_username.Text)
            //+ " and Hotel.acpassword=" 
            //+ SqlFormat.ap(TextBox_password.Text) + ";";
            object[] data=HotelUser.getDataByIDAndPsw(conn,TextBox_username.Text,TextBox_password.Text);
             if(data!=null&&data.Length>0){
                 userid = data[2].ToString();

                hotelid=data[0].ToString();

            }
            
            logintype = 2;
        } if (DropDownList_logintype.SelectedValue.ToString() == "admin")
        {
            string selsql = "SELECT AdminTable.account FROM AdminTable WHERE AdminTable.account="
            + SqlFormat.ap(TextBox_username.Text)
            + " and AdminTable.acpassword="
            + SqlFormat.ap(TextBox_password.Text) + ";";

            SqlDataAdapter sqlda = new SqlDataAdapter();
            
            sqlda.SelectCommand = new SqlCommand(selsql ,conn);
            DataSet sqlds = new DataSet();
            sqlda.Fill(sqlds);
          
            if (sqlds.Tables[0].Rows.Count == 1)
            {
                DataRow dr = sqlds.Tables[0].Rows[0];
                userid = dr[0].ToString().Trim();
                
            }

            logintype = 3;
        }
        conn.Close();
        //SqlDataAdapter sqlda = new SqlDataAdapter();
        //conn.Open();
        //sqlda.SelectCommand = new SqlCommand(sel, conn);    
        //DataSet sqlds = new DataSet(); 
        //sqlda.Fill(sqlds);
        //conn.Close();
        //if (sqlds.Tables[0].Rows.Count == 1)
        //{
        //    DataRow dr = sqlds.Tables[0].Rows[0];
        //    Session["userid"]=  dr[0].ToString().Trim();
        //    switch(logintype)
        //    {
        //        case 1:
        //             Session["usertype"]="agentsrub";
        //            break;
        //        case 2:
        //             Session["usertype"]="hotelsrub";
        //            break;
        //    }
        //    return true;
        //}

        if (userid!=null)
        {

            Session["userId"] = userid.Trim();
           
            switch (logintype)
            {
                case 1:
                    Session["usertype"] = "agentsrub";
                    break;
                case 2:
                    Session["usertype"] = "hotelsrub";
                    if (hotelid!=null)
                        Session["hotelid"] = hotelid.Trim();
                    break;
                case 3:
                    Session["usertype"] = "adminsrub";
                    break;
            }
            return true;
        }
        return false;
    }

    protected void Page_PreInit(object sender, EventArgs e)
    {
        if (this.IsPostBack) { }
        {
            HttpCookie cookie1 = new HttpCookie("themetype");
            cookie1.Value = "blue";
            Response.AppendCookie(cookie1);
        }
        if (Request.QueryString["NewTheme"] == "change")
        {
             Page.Theme = (Request.Cookies["themetype"]).Value;

        }
        
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = "欢迎访问SBRU预订系统";
    }
    protected void Button_login_Click(object sender, EventArgs e)
    {
        int ulength=TextBox_username.Text.Length;
        int plength=TextBox_password.Text.Length;
        bool OK = true;
        if (ulength < 3 || ulength > 20)
        {
           
            OK = false;
        }
        if (plength < 6 || plength > 16)
        {
          
            OK = false;
        }

        if (OK == true)
        {
            if (loginCheck() == true)
            {
                //Session["userId"] = TextBox_username.Text;
                Session["login"] = "havelogin";
                if ((String)Session["usertype"] == "agentsrub")
                {
                    Response.Redirect("~/AgentPage/AgentDefault.aspx");
                    return;
                }
                if ((String)Session["usertype"] == "hotelsrub")
                {
                    Response.Redirect("~/HotelPage/HotelInput.aspx");
                    return;
                }
                if ((String)Session["usertype"] == "adminsrub")
                {
                    Response.Redirect("~/AdminPage/Hotel.aspx");
                    return;
                }

            }
            else
            {
                Response.Write("<script language=javascript > alert('登陆失败，帐号密码错误！')</script>");
            }
          
        }
    

    }



    protected void Button_register_Click(object sender, ImageClickEventArgs e)
    {
        HttpCookie c2 = Request.Cookies["themetype"];
            if (c2.Value == "blue")
            {
                Response.Redirect("register.aspx?NewTheme=happy");
                c2.Value = "red";
                return;
            }else if(c2.Value == "red")
            {
                c2.Value = "blue";
                Response.Redirect("register.aspx?NewTheme=blue");
                return;
            }

       
    }

}